package io.qar.categories.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.qar.commons.mybatis.service.impl.CrudServiceImpl;
import io.qar.categories.dao.CategoriesDao;
import io.qar.categories.dto.CategoriesDTO;
import io.qar.categories.entity.CategoriesEntity;
import io.qar.categories.service.CategoriesService;
import io.qar.commons.tools.utils.ConvertUtils;
import io.qar.service.PageSerice;
import io.qar.utils.TreeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

//import io.qar.commons.lock.LockInfo;
//import io.qar.commons.lock.LockTemplate;
import java.util.List;
import java.util.Map;

/**
 * 分类信息表
 *
 * @author yyyyjinying 1020066395@qq.com
 * @since 3.0 2024-06-02
 */
@Service
public class CategoriesServiceImpl extends CrudServiceImpl<CategoriesDao, CategoriesEntity, CategoriesDTO> implements CategoriesService {
//    @Autowired
//    private RedisTemplate redisTemplate;

//    @Autowired
//    private LockTemplate lockTemplate;


    @Override
    @CacheEvict(value = "categories", allEntries = true)
    public void save(CategoriesDTO dto) {
        super.save(dto);
    }

    @Override
    @CacheEvict(value = "categories", allEntries = true)
    public void update(CategoriesDTO dto) {
        super.update(dto);
    }


    @Override
    @CacheEvict(value = "categories", allEntries = true)
    public void delete(Long[] ids) {
        super.delete(ids);
    }

    @Override
    @Cacheable(value = "categories", key = "#id")
    public CategoriesDTO get(Long id) {
        CategoriesEntity entity = baseDao.selectById(id);
        return ConvertUtils.sourceToTarget(entity, currentDtoClass());
    }

    @Override
    public QueryWrapper<CategoriesEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<CategoriesEntity> wrapper = new QueryWrapper<>();

        String name = (String) params.get("name");
        wrapper.eq(StringUtils.isNotBlank(name), "name", name);

        return wrapper;
    }


    @Override
    public List<CategoriesDTO> list(Map<String, Object> params) {
        List<CategoriesEntity> entityList = baseDao.getList(params);
        List<CategoriesDTO> categoriesDTOS = ConvertUtils.sourceToTarget(entityList, CategoriesDTO.class);
        return TreeUtils.build(categoriesDTOS);
    }

    @Override
    @Cacheable(value = "categories", key = "'totals'")
    public Long selectCount() {
        return baseDao.selectCount(null);
    }




//    @Override
//    public Long selectCount(){
//        Integer counts = (Integer) redisTemplate.opsForValue().get("toalCategories");
//        Long count = counts.longValue();
//        if (count != null ) {
//            return count;
//        } else {
//            Long counttatol = baseDao.selectCount(null);
//            redisTemplate.opsForValue().set("toalCategories", counttatol);
//            return count;
//        }
//    }
}